﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.InterferenceVolumeQuery.CUiInterferenceVolumeQueryDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.InterferenceVolumeQuery.Tree;
using System;
using System.CodeDom.Compiler;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.InterferenceVolumeQuery;

public class CUiInterferenceVolumeQueryDlg : TxWindow, IComponentConnector
{
  private IVQTreeListModel dataModel;
  private TxObjectList ivObjectsInTree;
  internal System.Windows.Controls.Button addButton;
  internal System.Windows.Controls.Button removeButton;
  internal ToggleButton highlightBtn;
  internal System.Windows.Controls.Button collapseButton;
  internal System.Windows.Controls.Button expandButton;
  internal TreeList tree;
  private bool _contentLoaded;

  public CUiInterferenceVolumeQueryDlg()
  {
    this.InitializeComponent();
    this.dataModel = new IVQTreeListModel();
    this.tree.Model = (ITreeModel) this.dataModel;
    this.tree.ExpandedValueChanged += new ExpandedValueChangedEventHandler(this.tree_ExpandedValueChanged);
    TxApplication.ActiveDocument.Selection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.Selection_ItemsAdded);
    TxApplication.ActiveDocument.Selection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.Selection_ItemsRemoved);
    TxApplication.ActiveDocument.Selection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.Selection_ItemsSet);
    TxApplication.ActiveDocument.Selection.Cleared += new TxSelection_ClearedEventHandler(this.Selection_Cleared);
    this.tree.SelectionChanged += new SelectionChangedEventHandler(this.tree_SelectionChanged);
    ((Window) this).Closing += new CancelEventHandler(this.CUiInterferenceVolumeQueryDlg_Closing);
    this.ShouldAutoPosition = true;
    this.ShouldCloseOnDocumentUnloading = true;
    this.ivObjectsInTree = new TxObjectList();
    TxApplication.ActiveUndoManager.StartTransaction();
    this.AddObjectsUponOpening();
  }

  private IIVQTreeViewItem RootNode
  {
    get
    {
      DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode treeNode = this.tree.SelectedNode;
      if (treeNode == null)
        return (IIVQTreeViewItem) null;
      while (treeNode.Parent.Parent != null)
        treeNode = treeNode.Parent;
      return treeNode.Tag as IIVQTreeViewItem;
    }
  }

  private void AddSelectedObjectsInTree()
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    TxObjectList filteredItems = TxApplication.ActiveDocument.Selection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxInterferenceVolume)));
    foreach (ITxObject itxObject in (Collection<ITxObject>) this.ivObjectsInTree)
    {
      if (((Collection<ITxObject>) filteredItems).Contains(itxObject))
        ((Collection<ITxObject>) filteredItems).Remove(itxObject);
    }
    if (((Collection<ITxObject>) filteredItems).Count > 0)
    {
      foreach (IIVQTreeViewItem treeViewItem in CApIVQManager.CreateTreeViewItems(filteredItems))
      {
        (this.dataModel.RootNode as IIVQTreeViewItem).Children.Add(treeViewItem);
        ((Collection<ITxObject>) this.ivObjectsInTree).Add(treeViewItem.IVQObject);
      }
    }
    Cursor.Current = current;
  }

  private void AddObjectsUponOpening()
  {
    this.AddSelectedObjectsInTree();
    this.addButton.IsEnabled = false;
    this.expandButton.IsEnabled = this.ShouldEnableExpandCollapse(new CUiInterferenceVolumeQueryDlg.ExpansionButtonsFunc(this.CanBeExpanded));
  }

  private bool ShouldEnableExpandCollapse(
    CUiInterferenceVolumeQueryDlg.ExpansionButtonsFunc func)
  {
    bool flag = false;
    foreach (DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode node in this.tree.Nodes)
    {
      flag = func(node);
      if (flag)
        break;
    }
    return flag;
  }

  private void EnableDisableExpandCollapseBtns()
  {
    this.collapseButton.IsEnabled = this.ShouldEnableExpandCollapse(new CUiInterferenceVolumeQueryDlg.ExpansionButtonsFunc(this.CanBeCollapsed));
    this.expandButton.IsEnabled = this.ShouldEnableExpandCollapse(new CUiInterferenceVolumeQueryDlg.ExpansionButtonsFunc(this.CanBeExpanded));
  }

  private void tree_ExpandedValueChanged() => this.EnableDisableExpandCollapseBtns();

  private bool CanBeCollapsed(DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode root)
  {
    if (root.IsExpanded)
      return true;
    foreach (DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode child in root.Children)
    {
      if (this.CanBeCollapsed(child))
        return true;
    }
    return false;
  }

  private bool CanBeExpanded(DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode root)
  {
    if (root.IsExpandable && !root.IsExpanded)
      return true;
    foreach (DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode child in root.Children)
    {
      if (this.CanBeExpanded(child))
        return true;
    }
    return false;
  }

  private void tree_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode selectedNode = this.tree.SelectedNode;
    if (this.removeButton.IsEnabled = selectedNode != null)
    {
      bool? isChecked = this.highlightBtn.IsChecked;
      bool flag = true;
      if (isChecked.GetValueOrDefault() == flag & isChecked.HasValue)
        CApIVQManager.HighLightObject(this.RootNode);
      CApIVQManager.ChangeSelection(selectedNode);
    }
    else
    {
      CApIVQManager.StopHighLighting();
      TxApplication.RefreshDisplay();
    }
  }

  private void EnableDisableButtons()
  {
    TxSelection selection = TxApplication.ActiveDocument.Selection;
    if (selection != null && selection.Count > 0)
      this.addButton.IsEnabled = ((Collection<ITxObject>) selection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxInterferenceVolume)))).Count > 0;
    else
      this.addButton.IsEnabled = false;
  }

  private void Selection_Cleared(object sender, TxSelection_ClearedEventArgs args)
  {
    this.addButton.IsEnabled = false;
  }

  private void Selection_ItemsSet(object sender, TxSelection_ItemsSetEventArgs args)
  {
    this.EnableDisableButtons();
  }

  private void Selection_ItemsRemoved(object sender, TxSelection_ItemsRemovedEventArgs args)
  {
    this.EnableDisableButtons();
  }

  private void Selection_ItemsAdded(object sender, TxSelection_ItemsAddedEventArgs args)
  {
    this.EnableDisableButtons();
  }

  private void CUiInterferenceVolumeQueryDlg_Closing(object sender, CancelEventArgs e)
  {
    CApIVQManager.StopHighLighting();
    TxApplication.RefreshDisplay();
    CApIVQManager.RemoveAllNotes();
    CApIVQManager.RestoreLocationsVisibility();
    TxApplication.ActiveDocument.Selection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.Selection_ItemsAdded);
    TxApplication.ActiveDocument.Selection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.Selection_ItemsRemoved);
    TxApplication.ActiveDocument.Selection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.Selection_ItemsSet);
    TxApplication.ActiveDocument.Selection.Cleared -= new TxSelection_ClearedEventHandler(this.Selection_Cleared);
    TxApplication.ActiveUndoManager.EndTransaction();
  }

  private void Button_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void removeButton_Click(object sender, RoutedEventArgs e)
  {
    if (this.tree.SelectedNode != null)
    {
      IIVQTreeViewItem rootNode = this.RootNode;
      ((IIVQTreeViewItem) this.tree.GetModelRootNode).Children.Remove(rootNode);
      CApIVQManager.RemoveNote(rootNode.IVQObject);
      ((Collection<ITxObject>) this.ivObjectsInTree).Remove(rootNode.IVQObject);
    }
    this.EnableDisableExpandCollapseBtns();
  }

  private bool CollapseOneLevelRec(DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode root)
  {
    if (!root.IsExpandable || !root.IsExpanded)
      return true;
    if (root.IsExpanded)
    {
      bool flag = true;
      foreach (DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode child in root.Children)
        flag = this.CollapseOneLevelRec(child) & flag;
      if (flag)
        root.IsExpanded = false;
    }
    return false;
  }

  private void ExpandOneLevelRec(DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode root)
  {
    if (!root.IsExpandable)
      return;
    if (!root.IsExpanded)
    {
      root.IsExpanded = true;
    }
    else
    {
      foreach (DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode child in root.Children)
        this.ExpandOneLevelRec(child);
    }
  }

  private void collapseButton_Click(object sender, RoutedEventArgs e)
  {
    foreach (DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode node in this.tree.Nodes)
      this.CollapseOneLevelRec(node);
  }

  private void expandButton_Click(object sender, RoutedEventArgs e)
  {
    foreach (DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode node in this.tree.Nodes)
      this.ExpandOneLevelRec(node);
  }

  private void addButton_Click(object sender, RoutedEventArgs e)
  {
    this.addButton.IsEnabled = false;
    this.AddSelectedObjectsInTree();
    this.expandButton.IsEnabled = this.ShouldEnableExpandCollapse(new CUiInterferenceVolumeQueryDlg.ExpansionButtonsFunc(this.CanBeExpanded));
  }

  private void highlightBtn_Click(object sender, RoutedEventArgs e)
  {
    bool? isChecked = this.highlightBtn.IsChecked;
    bool flag = true;
    if (isChecked.GetValueOrDefault() == flag & isChecked.HasValue)
    {
      CApIVQManager.HighLightObject(this.RootNode);
    }
    else
    {
      CApIVQManager.StopHighLighting();
      TxApplication.RefreshDisplay();
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/interferencevolumequery/cuiinterferencevolumequerydlg.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  internal Delegate _CreateDelegate(Type delegateType, string handler)
  {
    return Delegate.CreateDelegate(delegateType, (object) this, handler);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.addButton = (System.Windows.Controls.Button) target;
        this.addButton.Click += new RoutedEventHandler(this.addButton_Click);
        break;
      case 2:
        this.removeButton = (System.Windows.Controls.Button) target;
        this.removeButton.Click += new RoutedEventHandler(this.removeButton_Click);
        break;
      case 3:
        this.highlightBtn = (ToggleButton) target;
        this.highlightBtn.Click += new RoutedEventHandler(this.highlightBtn_Click);
        break;
      case 4:
        this.collapseButton = (System.Windows.Controls.Button) target;
        this.collapseButton.Click += new RoutedEventHandler(this.collapseButton_Click);
        break;
      case 5:
        this.expandButton = (System.Windows.Controls.Button) target;
        this.expandButton.Click += new RoutedEventHandler(this.expandButton_Click);
        break;
      case 6:
        this.tree = (TreeList) target;
        break;
      case 7:
        ((System.Windows.Controls.Primitives.ButtonBase) target).Click += new RoutedEventHandler(this.Button_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }

  private delegate bool ExpansionButtonsFunc(DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode root);
}
